/*
 * Copyright (c) 2025 彭友聪
 * nativePC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2. 
 * You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
 * See the Mulan PSL v2 for more details.  
 * 
 * Author: 彭友聪 
 * email：2923616405@qq.com 
 * date: 2025/10/8 09:27
 * file: ${FILE_NAME}
 * product: DevEco Studio
 * LICENSE: MulanPSL-2.0
 * */
//
// Created on 2025/10/8.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef NATIVEPC_NORMALTEXTLISTEXAMPLE_H
#define NATIVEPC_NORMALTEXTLISTEXAMPLE_H
#include "ArkUIButtonNode.h"
#include "NativeEntry.h"
#include "napi/native_api.h"
#include "ArkUIBaseNode.h"
#include "ArkUIListItemNode.h"
#include "ArkUIListNode.h"
#include "ArkUITextNode.h"
#include <hilog/log.h>



namespace NativeModule
{

    std::shared_ptr<ArkUIBaseNode> CreateTextListExample()
    {
        // 创建组件并挂载
        // 1：使用智能指针创建List组件。
        auto list = std::make_shared<ArkUIListNode>();
        list->SetPercentWidth(1);
        list->SetPercentHeight(1);
        list->SetScrollBarState(true);
        list->SetItemAlign(ARKUI_ITEM_ALIGNMENT_CENTER);
        list->SetListItemAlign(ARKUI_LIST_ITEM_ALIGNMENT_CENTER);
        // 创建一个按钮挂载到 list 组件上
        auto button = std::make_shared<ArkUIButtonNode>();
        button->SetEnable();
        button->SetPercentWidth(0.4);
        button->SetHeight(60);
        button->SetBackgroundColor(0xFF000000);
            // 2：创建ListItem子组件并挂载到List上。
            for (int32_t i = 0; i < 30; ++i) {
                auto listItem = std::make_shared<ArkUIListItemNode>();
                auto textNode = std::make_shared<ArkUITextNode>();
                textNode->SetTextContent(std::to_string(i));
                textNode->SetFontSize(16);
                textNode->SetPercentWidth(1);
                textNode->SetHeight(100);
                textNode->SetBackgroundColor(0xFFfffacd);
                textNode->SetTextAlign(ARKUI_TEXT_ALIGNMENT_CENTER);
                // 注册布局回调
                textNode->SetLayoutCallBack(i);
                // 注册绘制送显回调
                textNode->SetDrawCallBack(i);
                listItem->AddChild(textNode);
                // 列表项注册点击事件。
                auto onClick = [i](ArkUI_NodeEvent *event)
                {
                    std::string content = std::string("第" + std::to_string(i) + "个列表项被点击");
                    showUITextCallback("点击列表项", content);

                    // 从组件事件中获取基础事件对象
                    auto *inputEvent = OH_ArkUI_NodeEvent_GetInputEvent(event);
                        if (inputEvent == nullptr) {
                            return;
                        }
                    // 从组件事件获取事件类型
                    auto eventType = OH_ArkUI_NodeEvent_GetEventType(event);
                    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "eventInfo", "inputEvent = %{public}p",
                                 inputEvent);
                    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "eventInfo", "eventType = %{public}d", eventType);
                    auto componentEvent = OH_ArkUI_NodeEvent_GetNodeComponentEvent(event);
                    // 获取组件事件中的数字类型数据
                    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "eventInfo", "componentEvent = %{public}p",
                                 componentEvent);
                    // 获取触发该事件的组件对象
                    auto nodeHandle = OH_ArkUI_NodeEvent_GetNodeHandle(event);
                        if (nodeHandle == nullptr) {
                            return;
                        }
                    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "eventInfo", "nodeHandle = %{public}p",
                                 nodeHandle);
                        // 根据eventType来区分事件类型，进行差异化处理，其他获取事件信息的接口也可类似方式来进行差异化的处理
                        switch (eventType) {
                            case NODE_ON_CLICK_EVENT:
                                {
                                    // 触发点击事件所进行的操作，从基础事件获取事件信息
                                    auto x = OH_ArkUI_PointerEvent_GetX(inputEvent);
                                    auto y = OH_ArkUI_PointerEvent_GetY(inputEvent);
                                    auto displayX = OH_ArkUI_PointerEvent_GetDisplayX(inputEvent);
                                    auto displayY = OH_ArkUI_PointerEvent_GetDisplayY(inputEvent);
                                    auto windowX = OH_ArkUI_PointerEvent_GetWindowX(inputEvent);
                                    auto windowY = OH_ArkUI_PointerEvent_GetWindowY(inputEvent);
                                    auto pointerCount = OH_ArkUI_PointerEvent_GetPointerCount(inputEvent);
                                    auto xByIndex = OH_ArkUI_PointerEvent_GetXByIndex(inputEvent, 0);
                                    auto yByIndex = OH_ArkUI_PointerEvent_GetYByIndex(inputEvent, 0);
                                    auto displayXByIndex = OH_ArkUI_PointerEvent_GetDisplayXByIndex(inputEvent, 0);
                                    auto displayYByIndex = OH_ArkUI_PointerEvent_GetDisplayYByIndex(inputEvent, 0);
                                    auto windowXByIndex = OH_ArkUI_PointerEvent_GetWindowXByIndex(inputEvent, 0);
                                    auto windowYByIndex = OH_ArkUI_PointerEvent_GetWindowYByIndex(inputEvent, 0);
                                    auto pointerId = OH_ArkUI_PointerEvent_GetPointerId(inputEvent, 0);
                                    auto pressure = OH_ArkUI_PointerEvent_GetPressure(inputEvent, 0);
                                    auto action = OH_ArkUI_UIInputEvent_GetAction(inputEvent);
                                    auto eventTime = OH_ArkUI_UIInputEvent_GetEventTime(inputEvent);
                                    auto sourceType = OH_ArkUI_UIInputEvent_GetSourceType(inputEvent);
                                    auto type = OH_ArkUI_UIInputEvent_GetType(inputEvent);
                                    std::string eventInfo = "x: " + std::to_string(x) + ", y: " + std::to_string(y) +
                                                            ", displayX: " + std::to_string(displayX) +
                                                            ", displayY: " + std::to_string(displayY) +
                                                            ", windowX: " + std::to_string(windowX) +
                                                            ", windowY: " + std::to_string(windowY) +
                                                            ", pointerCount: " + std::to_string(pointerCount) +
                                                            ", xByIndex: " + std::to_string(xByIndex) +
                                                            ", yByIndex: " + std::to_string(yByIndex) +
                                                            ", displayXByIndex: " + std::to_string(displayXByIndex) +
                                                            ", displayYByIndex: " + std::to_string(displayYByIndex) +
                                                            ", windowXByIndex: " + std::to_string(windowXByIndex) +
                                                            ", windowYByIndex: " + std::to_string(windowYByIndex) +
                                                            ", pointerId: " + std::to_string(pointerId) +
                                                            ", pressure: " + std::to_string(pressure) +
                                                            ", action: " + std::to_string(action) +
                                                            ", eventTime: " + std::to_string(eventTime) +
                                                            ", sourceType: " + std::to_string(sourceType) +
                                                            ", type: " + std::to_string(type);
                                    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "eventInfoOfCommonEvent",
                                                 "eventInfo = %{public}s", eventInfo.c_str());
                                }
                            default:
                                {
                                    break;
                                }
                        }
                };
                listItem->RegisterOnClick(onClick);
                list->AddChild(listItem);
            }
        // 3：注册List相关监听事件.
        list->RegisterOnScrollIndex([](int32_t index)
                                    { OH_LOG_INFO(LOG_APP, "on list scroll index: %{public}d", index); });
        // 4: 注册挂载事件。
        list->RegisterOnAppear([]() { OH_LOG_INFO(LOG_APP, "on list mount to tree"); });
        // 5: 注册卸载事件。
        list->RegisterOnDisappear([]() { OH_LOG_INFO(LOG_APP, "on list unmount from tree"); });
        return list;
    }
}

#endif //NATIVEPC_NORMALTEXTLISTEXAMPLE_H
